#include <stdio.h>
#include <getopt.h>
#include <string>
#include "EventLoop.h"
#include "HttpServer.h"
#include "base/Logging.h"

int main(int argc, char **argv) {
	int threadNum = 1;
	int port = 8000;
	std::string log_path = "./WebServer.log";

	// parse args
	int opt;
	const char *str = "t:l:p:";
	while ((opt = getopt(argc, argv, str)) != -1)
	{
		switch (opt)
		{
		case 't': {
			threadNum = atoi(optarg);
			break;
		}
		case 'l': {
			log_path = optarg;
			if (log_path.size() < 2 || optarg[0] != '/')
			{
		          printf("logPath should start with \"/\"\n");
		          abort();
			}
			break;
		}
		case 'p': {
			port = atoi(optarg);
			break;
		}
		default:
			break;
		}
	}

	san::Logger::setLogFileName(log_path);

	#ifndef _PTHREADS
	san::LOG << "_PTHREADS is not defined !";
	#endif

	san::EventLoop mainLoop;
	san::HttpServer myHTTPServer(&mainLoop, threadNum, port);
	myHTTPServer.start();
	mainLoop.loop();
	return 0;
}
